export class Matrix {
    static getCenterIdx(arr) {
        // let peekSize = [arr.length, arr[0].length]
        // return peekSize.map(e => Math.floor(e / 2))
        return [Math.floor(arr.length / 2), Math.floor(arr[0].length / 2)]
    }

    static transpose(arr) {
        return arr[0].map((_, i) => arr.map(row => row[i]))
    }

    static initByFor(row = 3, col = 4, callback) {
        /* 方法1：基于 for 初始化二维数组 */
        let arr = new Array(row)
        for (let x = 0; x < arr.length; x++) {
            arr[x] = new Array(col)
            for (let y = 0; y < arr[x].length; y++)
                arr[x][y] = callback()
        }
        return arr
    }

    static initByForIn(row = 3, col = 4, callback) {
        /* 方法2：基于 for/in 初始化二维数组 */
        let arr = new Array(row).fill(null)
        for (let x in arr) {
            arr[x] = new Array(col).fill(null)
            for (let y in arr[x])
                arr[x][y] = callback()
        }
        return arr
    }

    static initByForEach(row = 3, col = 4, callback) {
        /* 方法3：基于 forEach 初始化二维数组 */
        let arr = new Array(row).fill(null)
        arr.forEach((_, x) => {
            arr[x] = new Array(col).fill(null)
            arr[x].forEach((_, y) => {
                arr[x][y] = callback()
            })
        })
        return arr
    }

    static initByMap(row = 3, col = 4, callback) {
        /* 方法4：基于 map 初始化二维数组 */
        let arr = new Array(row).fill(null)
        return arr.map(_ => {
            let arr = new Array(col).fill(null)
            return arr.map(callback)
        })
    }
}
